/* $Id: PageProblem.java 138 2011-09-01 17:51:37Z altdotua@gmail.com $ */

package alt.djudge.frontend.client.ui.contest;

import alt.djudge.frontend.client.ui.ContentPage;
import alt.djudge.frontend.shared.dto.contest.ProblemStatementInfoDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;


public class PageProblem extends Composite implements ContentPage
{
	interface MyUiBinder extends UiBinder<VerticalPanel, PageProblem> {}

	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

	@UiField
	VerticalPanel panel;
	
	@UiField
	TabLayoutPanel tabs;
	
	@UiField
	Label lblTitle;
	
	public PageProblem()
	{
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	public void onDataLoaded(ProblemStatementInfoDTO[] data)
	{
		tabs.clear();
		
		lblTitle.setText("Problem statement");

		for (ProblemStatementInfoDTO problem : data)
		{
			HTML html = new HTML(problem.getStatement());
			TextArea area = new TextArea();
			area.setText(problem.getStatement());
			tabs.add(html, problem.getLocaleName());
		}
	}
	
	public void loadData(Long contestProblemId)
	{
		Layout.contestService.getStatements(contestProblemId, new AsyncCallback<ProblemStatementInfoDTO[]>()
		{
			@Override
			public void onSuccess(ProblemStatementInfoDTO[] result)
			{
				onDataLoaded(result);
			}
			
			@Override
			public void onFailure(Throwable caught)
			{
				Window.alert(caught.toString());
			}
		});
	}

	@Override
	public void dispatch(String token)
	{
		if (token.startsWith("problem-"))
		{
			Long problemId = Long.parseLong(token.replaceAll("[\\D]", ""));
			loadData(problemId);
		}
		else
		{
			Window.alert("Wrong history token for this page:\n" + token);
		}
	}

	@Override
	public String getHistoryTokenPrefix()
	{
		return "problem";
	}
}

